.TH std::basic_string::contains 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_string::contains \- std::basic_string::contains

.SH Synopsis
   constexpr bool
       contains( std::basic_string_view<CharT,Traits> sv ) const      \fB(1)\fP (since C++23)
   noexcept;
   constexpr bool                                                     \fB(2)\fP (since C++23)
       contains( CharT ch ) const noexcept;
   constexpr bool                                                     \fB(3)\fP (since C++23)
       contains( const CharT* s ) const;

   Checks if the string contains the given substring. The substring may be one of the
   following:

   1) A string view sv (which may be a result of implicit conversion from another
   std::basic_string).
   2) A single character ch.
   3) A null-terminated character string s.

   All three overloads are equivalent to return find(x) != npos;, where x is the
   parameter.

.SH Parameters

   sv - a string view which may be a result of implicit conversion from another
        std::basic_string
   ch - a single character
   s  - a null-terminated character string

.SH Return value

   true if the string contains the provided substring, false otherwise.

.SH Notes

      Feature-test macro      Value    Std        Feature
   __cpp_lib_string_contains 202011L (C++23) contains functions

.SH Example


// Run this code

 #include <iomanip>
 #include <iostream>
 #include <string>
 #include <string_view>
 #include <type_traits>

 template<typename SubstrType>
 void test_substring(const std::string& str, SubstrType subs)
 {
     constexpr char delim = std::is_scalar_v<SubstrType> ? '\\'' : '\\"';
     std::cout << std::quoted(str)
               << (str.contains(subs) ? " contains "
                                      : " does not contain ")
               << std::quoted(std::string{subs}, delim) << '\\n';
 }

 int main()
 {
     using namespace std::literals;

     auto helloWorld = "hello world"s;

     test_substring(helloWorld, "hello"sv);
     test_substring(helloWorld, "goodbye"sv);
     test_substring(helloWorld, 'w');
     test_substring(helloWorld, 'x');
 }

.SH Output:

 "hello world" contains "hello"
 "hello world" does not contain "goodbye"
 "hello world" contains 'w'
 "hello world" does not contain 'x'

.SH See also

   starts_with checks if the string starts with the given prefix
   (C++20)     \fI(public member function)\fP
   ends_with   checks if the string ends with the given suffix
   (C++20)     \fI(public member function)\fP
   find        finds the first occurrence of the given substring
               \fI(public member function)\fP
   substr      returns a substring
               \fI(public member function)\fP
   contains    checks if the string view contains the given substring or character
   (C++23)     \fI(public member function of std::basic_string_view<CharT,Traits>)\fP
